package cn.quan.dlairport;

import java.util.List;
import java.util.Map;

import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;

import android.util.Log;


public class DlairportParserHandler implements ContentHandler {
	private boolean mFlightTable = false;
	private boolean mFlightTR = false;
	private int mTdIndex = 0;
	//data
	FlightItem mItem ;

	
	private final int INDEX_FLIGHT_NO = 0;
	private final int INDEX_FLIGHT_FROM = 2;
	private final int INDEX_FLIGHT_TO = 6;
	private final int INDEX_FLIGHT_START = 7;
	private final int INDEX_FLIGHT_START_REAL = 9;
	private final int INDEX_FLIGHT_END = 10;
	private final int INDEX_FLIGHT_END_REAL = 12;
	private final int INDEX_FLIGHT_STATUS = 13;
	private final int INDEX_FLIGHT_REASON = 14;
//	private Map items;
	private List<FlightItem> mItems ;

	public DlairportParserHandler(List<FlightItem> items) {
		super();
		this.mItems = items;
		mFlightTable = false;
		mFlightTR = false;
		mTdIndex = 0;

	}

	@Override
	public void characters(char[] arg0, int arg1, int arg2) throws SAXException {
		// TODO Auto-generated method stub
		if(mFlightTable && mFlightTR){

			switch(mTdIndex){
				case INDEX_FLIGHT_NO://航班号
					mItem.flightNo+=(new String(arg0,arg1,arg2)).trim();
				break;
				case INDEX_FLIGHT_FROM://始发站
					mItem.flightFrom=new String(arg0,arg1,arg2);
					break;
				case INDEX_FLIGHT_TO://目的站
					mItem.flightTo=new String(arg0,arg1,arg2);
					break;
				case INDEX_FLIGHT_START://预起
					mItem.flightStart="预起"+(new String(arg0,arg1,arg2));
					break;
				case INDEX_FLIGHT_START_REAL://实起
					mItem.flightStartReal="实起"+(new String(arg0,arg1,arg2));
					break;
				case INDEX_FLIGHT_END://预降
					mItem.flightEnd="预降"+(new String(arg0,arg1,arg2));
					break;
				case INDEX_FLIGHT_END_REAL://实降
					mItem.flightEndReal="实降"+(new String(arg0,arg1,arg2));
					break;
				case INDEX_FLIGHT_STATUS://航班状态
					mItem.flightStatus=new String(arg0,arg1,arg2);
					break;
				case INDEX_FLIGHT_REASON://延误原因
					mItem.flightReason=new String(arg0,arg1,arg2);
					mItems.add(mItem);
					break;
			}
			
		}
	}

	@Override
	public void endDocument() throws SAXException {
		// TODO Auto-generated method stub
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		// TODO Auto-generated method stub
		String tag = qName;
		if(mFlightTable){
			if(tag.equals("table")){
				//parse end
				mFlightTable=false;
				mFlightTR = false;
			}else
			if(mFlightTR){
				if(tag.equals("tr")){
					mTdIndex = 0;
					mFlightTR = false;
				}else
				if(tag.equals("td")){
					mTdIndex++;
				} 
			}
		}
	}

	@Override
	public void endPrefixMapping(String prefix) throws SAXException {
		// TODO Auto-generated method stub
	}

	@Override
	public void ignorableWhitespace(char[] ch, int start, int length)
			throws SAXException {
		// TODO Auto-generated method stub
	}

	@Override
	public void processingInstruction(String target, String data)
			throws SAXException {
		// TODO Auto-generated method stub
	}

	@Override
	public void setDocumentLocator(Locator locator) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void skippedEntity(String name) throws SAXException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void startDocument() throws SAXException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes atts) throws SAXException {
		// TODO Auto-generated method stub
		String tag = qName;
		String att;
		String val;
		if(!mFlightTable ){
			if(tag.equals("table")){
				for(int i=0;i<atts.getLength();i++){
					att = atts.getQName(i);
					val = atts.getValue(i);
					if(att.equals("id")){
						if(val.equals("GridView1")){
							//parse start
							mFlightTable=true;
							break;
						}
					}
				}
			}
		}else if(!mFlightTR){
			if(tag.equals("tr")){
				//parse start
				mFlightTR=true;
				mItem = new FlightItem();
				mItem.clear();
			}
		}
	}

	@Override
	public void startPrefixMapping(String prefix, String uri)
			throws SAXException {
		// TODO Auto-generated method stub
		
	}
    private final String TAG = "**FLIGHT";

}
